package com.sc.nft.admin.controller;

import cn.hutool.core.collection.CollUtil;
import com.sc.nft.admin.request.IdRequest;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.dao.UserPredictEcologyHatchRewardsRecordsDao;
import com.sc.nft.dto.UserEcologyHatchRecordsDao;
import com.sc.nft.entity.SysMember;
import com.sc.nft.entity.UserEcologyCastUpperLimitDetails;
import com.sc.nft.entity.UserEcologyHatchRecords;
import com.sc.nft.entity.UserPredictEcologyHatchRewardsRecords;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.SysMemberPool;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.*;
import com.sc.nft.sup.Result;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 熔炼脚本
 *
 * @Author：LiGen
 * @Package：com.sc.nft.admin.controller
 * @Project：sc_metago
 * @name：EcologyHatchScriptController
 * @Date：2024/6/24 15:20
 * @Filename：EcologyHatchScriptController
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/ecologyHatch")
public class EcologyHatchScriptController {

    private final EcologyHatchScriptService ecologyHatchScriptService;
    private final Redisson redisson;
    private final AdminEcologyIncomeConfService adminEcologyIncomeConfService;
    private final UserPredictEcologyHatchRewardsRecordsService userPredictEcologyHatchRewardsRecordsService;
    private final UserEcologyHatchRecordsDao userEcologyHatchRecordsDao;
    private final UserEcologyHatchIncomeService userEcologyHatchIncomeService;
    private final UserPredictEcologyHatchRewardsRecordsDao userPredictEcologyHatchRewardsRecordsDao;
    private final CopyrightAuctionRoundService copyrightAuctionRoundService;




    @PostMapping(value = "/script", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "脚本服务", notes = "脚本服务", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
//    @SysMemberLogin
    public Result script() {
        copyrightAuctionRoundService.auctionDisposal();
        return Result.success();
    }

    @PostMapping(value = "/snapshotScript", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "快照脚本服务", notes = "快照脚本服务", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result snapshotScript() {
        SysMember member = SysMemberPool.getMember();
        if (!member.getId().equals(86L)) {
            throw new GlobalRunTimeException("无权限");
        }
        RLock lock = redisson.getLock("script" + member.getId());
        try {
            boolean res = lock.tryLock();
            if (res) {
                ecologyHatchScriptService.snapshotScript();
            } else {
                return Result.fail("数据同步已经开始：同步人ID：" + member.getId());
            }
        } catch (Exception e) {
            throw new GlobalRunTimeException("数据处理异常" + e.getMessage());
        } finally {
            lock.unlock();
        }
        return Result.success();
    }

    @PostMapping(value = "/userEcologyHatchRecordsDataProcessing", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "用户熔炼记录脚本服务", notes = "用户熔炼记录脚本服务", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result userEcologyHatchRecordsDataProcessing(@Valid @RequestBody IdRequest idRequest) {
//        ecologyHatchScriptService.userEcologyHatchRecordsDataProcessing(idRequest.getId());
        return Result.success();
    }


//    @PostMapping(value = "/releaseFrozenPoints", produces = MediaType.APPLICATION_JSON_VALUE)
//    @ApiOperation(value = "释放冻结积分", notes = "释放冻结积分", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
//    public Result releaseFrozenPoints() {
//        ecologyHatchScriptService.releaseFrozenPoints();
//        return Result.success();
//    }


}
